package com.jumi.microservice.service.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.dto.admin.CapitalDetailsRequest;
import com.jumi.microservice.dto.admin.CapitalDetailsResponse;
import com.jumi.microservice.dto.admin.User;
import com.jumi.microservice.dto.reponse.JmUserReponse;
import com.jumi.microservice.entity.UserWallet;
import com.jumi.microservice.mapper.UserWalletMapper;
import com.jumi.microservice.service.IMemberService;
import com.jumi.microservice.service.IUserExternalService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wangjingdao
 */
@Service
public class AdminCapitalDetailsService {
    @Resource
    UserWalletMapper userWalletMapper;
    @DubboReference
    IMemberService iMemberService;
    @DubboReference
    private IUserExternalService iUserExternalService;
    @Resource
    WalletAccountService walletAccountService;


    public TableDataInfo<CapitalDetailsResponse> getCapitalList(CapitalDetailsRequest request) {
        TableDataInfo<CapitalDetailsResponse> dataInfo = new TableDataInfo<>();
        QueryWrapper<UserWallet> queryWrapper = new QueryWrapper<>();
        if (request.getPhone() != null) {
            Integer uid = iUserExternalService.getUidByMobile(request.getPhone());
            if (uid == null) {
                dataInfo.setRows(new ArrayList<>());
                dataInfo.setTotal(0L);
                return dataInfo;
            }
            queryWrapper.eq("user_id", uid);
        }
        if (request.getMembershipLevel() != null) {
            queryWrapper.eq("user_level", request.getMembershipLevel());
        }
        if (request.getType() != null) {
            queryWrapper.eq("type", request.getType());
        }
        if (request.getStartTime() != null && request.getEndTime() != null) {
            queryWrapper.between("create_time", request.getStartTime(), request.getEndTime());
        }
        queryWrapper.orderByDesc("create_time");
        IPage<UserWallet> page = new Page<>(request.getPageNum(), request.getPageSize());
        page = userWalletMapper.selectPage(page, queryWrapper);
        List<UserWallet> wallets = page.getRecords();
        if(wallets.isEmpty()){
            dataInfo.setRows(new ArrayList<>());
            dataInfo.setTotal(0L);
            return dataInfo;
        }
        List<CapitalDetailsResponse> capitalDetailsResponses = new ArrayList<>();
        Map<Long, JmUserReponse> userMap = getUserInfoMap(wallets.stream().map(w -> w.getUserId().intValue()).collect(Collectors.toList()));
        Map<Integer,Object> levelMap = walletAccountService.memberLevel();
        wallets.forEach(
                userWallet -> {
                    CapitalDetailsResponse capitalDetailsResponse = new CapitalDetailsResponse();
                    capitalDetailsResponse.setUid(userWallet.getUserId().intValue());
                    BeanUtils.copyProperties(userWallet, capitalDetailsResponse);
                    if (userMap.containsKey(userWallet.getUserId())) {
                        User user = new User();
                        user.setHeadUrl(userMap.get(userWallet.getUserId()).getAvatar());
                        user.setTrueName(userMap.get(userWallet.getUserId()).getTruename());
                        user.setNickName(userMap.get(userWallet.getUserId()).getName());
                        user.setTime(userMap.get(userWallet.getUserId()).getCreateTime());
                        capitalDetailsResponse.setUid(userMap.get(userWallet.getUserId()).getId());
                        capitalDetailsResponse.setUser(user);
                        capitalDetailsResponse.setPhone(userMap.get(userWallet.getUserId()).getMobile());
                        capitalDetailsResponse.setMembershipLevel("");
                        if(levelMap.containsKey(userWallet.getUserLevel())){
                            capitalDetailsResponse.setMembershipLevel(levelMap.get(userWallet.getUserLevel()).toString());
                        }
                    }
                    capitalDetailsResponses.add(capitalDetailsResponse);
                }
        );
        dataInfo.setRows(capitalDetailsResponses);
        dataInfo.setTotal(page.getTotal());
        return dataInfo;
    }

    /**
     * 获取所有用户信息
     *
     * @param userIds
     * @return
     */
    public Map<Long, JmUserReponse> getUserInfoMap(List<Integer> userIds) {
        if(userIds.isEmpty()){
            return new HashMap<>();
        }
        List<JmUserReponse> list = iMemberService.getMemberByUids(userIds).getData();
        Map<Long, JmUserReponse> map = new HashMap<>(list.size());
        for (JmUserReponse jmUserReponse : list) {
            map.put(jmUserReponse.getId().longValue(), jmUserReponse);
        }
        return map;
    }
}
